<?php
namespace app\index\controller;
use app\facade\Redis;
use think\Db;
use app\models\User;

class Acticle extends Base
{

    public function list(){
        $uid = $this->request->user_id;
        $pageSize = $this->request->post("pageSize");
        $page = $this->request->post("page");
        $status = $this->request->post("status");
        $type= $this->request->post("type");
        if (!$pageSize){
            $pageSize = 10;
        }
        if (!$page){
            $page = 1;
        }
        $where =array();
        if ($status){
            $where["status"] = $status;
        }
        if ($type){
            $where["type"] = $type;
        }
        $datas =  Db::table('tb_article')->where($where)->field("id,title,introduce,time")->order("time desc")->page($page)->paginate($pageSize);
        $list = $datas->all();
        foreach ($list as $k => $v){
            $read = Redis::hget("acticle_read",$uid.":".$v["id"]);
            $list[$k]['read'] = $read;
            $datas[$k] = $list[$k];
        }
        return $this->sendSuccess("获取成功",$datas);
    }

    public function detail(){
    	$id = $this->request->post("id");
        $uid = $this->request->user_id;
        $user= $this->request->user;
    	$data= Db::table('tb_article')->where("id",$id)->find();
        if ($data){
            Redis::hset("acticle_read",$uid.":".$id,1);
            $data["uid"]=$uid;
//            $data["user"]=$user;
            return $this->sendSuccess("获取成功",$data);
        }
        return $this->sendFail("没有找到数据");
    }

    public function listOther(){
        $uid = $this->request->user_id;
        $pageSize = $this->request->post("pageSize");
        $page = $this->request->post("page");
        $status = $this->request->post("status");
        $type= $this->request->post("type");
        if (!$pageSize){
            $pageSize = 10;
        }
        if (!$page){
            $page = 1;
        }
        $where =array();
        if ($status){
            $where["status"] = $status;
        }
        if ($type){
            $where["type"] = $type;
        }
        $datas =  Db::table('tb_article')->where($where)->field("id,title,introduce,time")->order("time desc")->page($page)->paginate($pageSize);
        return $this->sendSuccess("获取成功",$datas);
    }

    public function detailOther(){
        $id = $this->request->post("id");
        $data= Db::table('tb_article')->where("id",$id)->find();
        return $this->sendSuccess("获取成功",$data);
    }

    public function workPost(){
        $data =array();
        $uid = $this->request->user_id;
        $id = $this->request->post("id");
        $data["quesion"]= $this->request->post("quesion");
        if (empty($data["quesion"])){
            return $this->sendFail("问题不能为空");
        }
        $data["mobile"]= $this->request->post("mobile");
        $data["imgs"]= $this->request->post("imgs");
        if (empty($id)){
            $data["uid"]= $uid;
            $data["time"]= time();
            $data["status"]= 0;
            Db::table('tb_work')->insert($data);
        }else{
            Db::table('tb_work')->where("id",$id)->update($data);
        }
        return $this->sendSuccess("获取成功",$data);
    }

    public function workList(){
        $uid = $this->request->user_id;
        $pageSize = $this->request->post("pageSize");
        $page = $this->request->post("page");
        $status = $this->request->post("status");
        if (!$pageSize){
            $pageSize = 10;
        }
        if (!$page){
            $page = 1;
        }
        $where =array();
        $where["uid"] =$uid;
        if ($status){
            $where["status"] = $status;
        }
        $datas =  Db::table('tb_work')->where($where)->order("id desc")->page($page)->paginate($pageSize);
        return $this->sendSuccess("获取成功",$datas);
    }

    public function workDetail(){
        $id = $this->request->post("id");
        $uid = $this->request->user_id;
        $data= Db::table('tb_work')->where("id",$id)->where("uid",$uid)->find();
        if (!$data){
            return $this->sendFail("您没有查看此工单的权限");
        }
        return $this->sendSuccess("获取成功",$data);
    }

}